package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.DriverSCM;
import com.zr.service.EmployeeServiceSCM;






/**
 * @author  shichongming
 * @E-mail: 
 * @date 创建时间：2017年7月7日 下午7:50:05 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */

public class DriverActionSCM extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private Map<String, Object> session;
	private int rows;
	private int page;	
	private int driverId;	
	private DriverSCM d;
	private String  driverPhone;
	private String upsw;
	private int rid;
	private String ustate;
	


	
	
	//属性注入
		@Resource
		private EmployeeServiceSCM employeeService;
		
	/**
	 * 展示司机
	 * @throws IOException 
	 * */
	public void driverShow() throws IOException{			
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");	
		int total = employeeService.getDrivertotal();	
		JSONArray js=employeeService.findDriver(page, rows);	
		JSONObject j=new JSONObject();		
		j.put("total",total);
		j.put("rows",js);
		PrintWriter pw =resp.getWriter();
		pw.write(j.toString());	
		pw.close();
	}
	
	
	/**
	 * 司机入职
	 * @throws IOException 
	 * */
	public void driverAdd() throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");	
		int i=employeeService.addDriver(d.getDriverName(), d.getDriverPhone(), d.getUname(),d.getUpsw());
		int m=employeeService.addDriverRole(d.getUname(),d.getUpsw() ,d.getRid());
		PrintWriter pw =resp.getWriter();
		if(i>0&m>0){
			pw.write("1");	
			pw.close();
		}else{
			pw.write("0");	
			pw.close();
		}
	}
	
	
	/**
	 * 司机离职修改状态
	 * @throws UnsupportedEncodingException 
	 * */
	
	public void deleteDriver() throws UnsupportedEncodingException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");			
		employeeService.deleteDriver(driverId);		
	}
	
	
	/**
	 * 修改司机信息
	 * @throws IOException 
	 * */
	public void updateDriver() throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		int uid=employeeService.updateDriver(driverId, driverPhone, upsw, ustate);
		int i=employeeService.updateDriverRole(uid,rid);
		PrintWriter pw =resp.getWriter();
		if(i>0){
			pw.write("1");	
			pw.close();
		}else{
			pw.write("0");	
			pw.close();
		}
		
	}
	
	
	
	
	
	
	
	
	
	public String getDriverPhone() {
		return driverPhone;
	}


	public void setDriverPhone(String driverPhone) {
		this.driverPhone = driverPhone;
	}


	public String getUpsw() {
		return upsw;
	}


	public void setUpsw(String upsw) {
		this.upsw = upsw;
	}


	public int getRid() {
		return rid;
	}


	public void setRid(int rid) {
		this.rid = rid;
	}


	public String getUstate() {
		return ustate;
	}


	public void setUstate(String ustate) {
		this.ustate = ustate;
	}


	public int getDriverId() {
		return driverId;
	}


	public void setDriverId(int driverId) {
		this.driverId = driverId;
	}


	public DriverSCM getD() {
		return d;
	}
	public void setD(DriverSCM d) {
		this.d = d;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}


	public void setPage(int page) {
		this.page = page;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp=arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.req=arg0;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.session=arg0;
	}





	
	
	
}
